package com.tjs.lucky.infrastructure.repository;

import com.tjs.lucky.domain.award.repository.IAwardRepository;
import com.tjs.lucky.infrastructure.mapper.IActivityMapper;
import com.tjs.lucky.infrastructure.mapper.IAwardMapper;
import com.tjs.lucky.infrastructure.mapper.IStrategyDetailMapper;
import com.tjs.lucky.infrastructure.mapper.IUserStrategyExportMapper;
import com.tjs.lucky.infrastructure.po.Award;
import com.tjs.lucky.infrastructure.po.StrategyDetail;
import com.tjs.lucky.infrastructure.po.UserStrategyExport;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 田佳澍
 * @Description:
 * @DateTime: 2023/5/22 16:26
 **/
@Repository
public class AwardRepository implements IAwardRepository {

    @Resource
    private IAwardMapper awardMapper;

    @Resource
    private IStrategyDetailMapper strategyDetailMapper;

    @Resource
    private IActivityMapper activityMapper;

    @Resource
    private IUserStrategyExportMapper userStrategyExportMapper;

    @Override
    public void updateUserAwardState(String uId, Long orderId, String awardId, Integer grantState) {
        UserStrategyExport userStrategyExport = new UserStrategyExport();
        userStrategyExport.setUId(uId);
        userStrategyExport.setOrderId(orderId);
        userStrategyExport.setAwardId(awardId);
        userStrategyExport.setGrantState(grantState);
        userStrategyExportMapper.updateUserAwardState(userStrategyExport);
    }

    @Override
    public Map<String, String> queryAwardList(Long activityId) {
        Long strategyId = activityMapper.queryStrategyIdByActivityId(activityId);
        List<StrategyDetail> strategyDetails = strategyDetailMapper.queryByStrategyId(strategyId);
        Map<String, String> result = new HashMap<>();
        for (StrategyDetail strategyDetail : strategyDetails) {
            result.put(strategyDetail.getAwardId(), String.valueOf(strategyDetail.getAwardCount()));
        }
        return result;
    }
}
